import { ColliderType, ColliderUpdateType, CollisionGroup } from "../../GameDef";
import { BaseShape } from "../../lib/kgame/d2/shape/BaseShape";
import { ECSCmp } from "../../lib/kgame/ecs/ECSCmp";

/**
 * 碰撞组件
 */
export class ColliderCmp<Shape extends BaseShape = BaseShape> extends ECSCmp {

    /** 碰撞器图形 */
    shape: Shape;
    /** 碰撞器类型 */
    type: ColliderType;
    /** 碰撞组 */
    group: CollisionGroup;
    /** 跳过id 相同id将跳过检测 */
    passId: number;
    /** 碰撞器更新类型 */
    updateType: ColliderUpdateType;

    /** 这一帧检测到自己碰撞到的所有碰撞器组件 */
    collidersOther: ColliderCmp[] = [];
    /** 这一帧检测到碰撞到自己的所有碰撞器组件 */
    collidersSelf: ColliderCmp[] = [];

    /**
     * 初始化碰撞组件
     * @param shape 碰撞器图形 
     * @param type 碰撞器类型 
     * @param group 碰撞组 
     * @param passId 跳过id 相同id将跳过检测 
     * @param updateType 碰撞器更新类型
     */
    onCreate(shape: Shape, type: ColliderType, group: CollisionGroup, passId: number, updateType = ColliderUpdateType.update) {
        this.shape = shape;
        this.type = type;
        this.group = group;
        this.passId = passId;
        this.updateType = updateType;
        return this;
    }

    protected onClear(): void {
        this.shape = null;
        this.collidersOther.length = 0;
        this.collidersSelf.length = 0;
    }

}